﻿#include "VTKOutput.h"
#include "System.h"
#include "Timer.h"
#include "Transient.h"

#include <fmt/format.h>
#include <vtkm/Version.h>
#include <vtkm/io/VTKDataSetWriter.h>

RegisterObject(VTKOutput);

VTKOutput::VTKOutput(const Configuration& cfg)
  : FileOutput(cfg)
  , _binary(Get<bool>("binary", false))
{
}

void VTKOutput::Init() {}

void VTKOutput::Execute()
{
  Timer timer;
  timer.Start();
  if (ShouldOutput())
  {
    try
    {
      vtkm::io::VTKDataSetWriter writer(FileName());
      auto vtkm_version = std::stof(VTKM_VERSION);
      //if (VTKM_VERSION_MINOR >= 7)
      if (_binary)
      {
        writer.SetFileTypeToBinary();
      }
      //writer.SetFileTypeToBinary();
      writer.WriteDataSet(_system.GetDateSet());
    }
    catch (const std::exception& e)
    {
      Error(e.what());
    }
    Success("VTKOutput::Execute, Time Elapsed: ", timer.GetElapsedTime());
  }
}

std::string VTKOutput::FileName()
{
  auto current_step = _executioner.CurrentStep();
  std::string file_name = fmt::format("{0}_{1:0>5}.{2}", _file_base, current_step, "vtk");
  return file_name;
}
